package com.edgclub.edg.uc.galleryviewpager;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.net.Uri;
import android.os.AsyncTask;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView.ScaleType;

import com.edgclub.edg.R;
import com.edgclub.edg.utils.GlideUtil.ImageLoaderClient;
import com.edgclub.edg.utils.util.BitmapUtility;
import com.edgclub.edg.utils.util.StringUtils;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;

public class UrlPagerAdapterforImage extends BasePagerAdapter {

	private ImageLoaderClient imageLoaderClient;

	HashMap<Integer, UrlTouchImageViewForImage> map;

	private Context mContext;

	public UrlPagerAdapterforImage(Context context, ArrayList<String> resources) {
		super(context, resources);

		imageLoaderClient = new ImageLoaderClient(mContext);

		mContext = context;
		map = new HashMap<Integer, UrlTouchImageViewForImage>();
	}

	@Override
	public void setPrimaryItem(ViewGroup container, final int position,
			Object object) {
		super.setPrimaryItem(container, position, object);
		((GalleryViewPager) container).mCurrentView = ((UrlTouchImageViewForImage) object)
				.getImageView();
	}

	@Override
	public Object instantiateItem(ViewGroup collection, final int position) {
		UrlTouchImageViewForImage iv;
		if (map.containsKey(position)) {
			iv = map.get(position);
		} else {
			iv = new UrlTouchImageViewForImage(mContext);
			map.put(position, iv);
		}

		if (mResources.get(position).endsWith("gif")){
			iv.setUrl(mResources.get(position));
		} else {
			new LoadBitmapTask(iv, mResources.get(position).replace("file://", ""))
					.execute();
		}

//		new LoadBitmapTask(iv, mResources.get(position).replace("file://", ""))
//				.execute();

		// iv.setUrl(mResources.get(position));
		iv.setLayoutParams(new ViewGroup.LayoutParams(
				ViewGroup.LayoutParams.MATCH_PARENT,
				ViewGroup.LayoutParams.MATCH_PARENT));

		collection.addView(iv, 0);

		return iv;
	}

	private class LoadBitmapTask extends AsyncTask<Void, Void, Bitmap> {

		private UrlTouchImageViewForImage iv;

		private String path;

		public LoadBitmapTask(UrlTouchImageViewForImage iv, String path) {
			super();
			this.iv = iv;
			this.path = path;
		}

		@Override
		protected void onPostExecute(Bitmap result) {
			super.onPostExecute(result);
			if (result != null) {

				try {
					iv.mImageView.setVisibility(View.VISIBLE);
					iv.tvProgress.setVisibility(View.GONE);
					iv.mImageView.setScaleType(ScaleType.MATRIX);
					iv.mImageView.setImageBitmap(result);
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
					iv.mImageView.setScaleType(ScaleType.CENTER);
					iv.tvProgress.setText(StringUtils.getString(R.string.imageloadingcontent1));
				}
			} else {
				iv.mImageView.setScaleType(ScaleType.CENTER);
				iv.tvProgress.setText(StringUtils.getString(R.string.imageloadingcontent1));
			}
		}

		@Override
		protected void onPreExecute() {
			iv.mImageView.setScaleType(ScaleType.CENTER);
			iv.tvProgress.setText(StringUtils.getString(R.string.imageloadingcontent2));
			super.onPreExecute();
		}

		@Override
		protected Bitmap doInBackground(Void... arg0) {
			Bitmap bitmap = null;
			try {
				bitmap = BitmapUtility.getBitmapFromUri2(mContext,
						Uri.fromFile(new File(path)));
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			return bitmap;
		}

	}

	@Override
	public void destroyItem(ViewGroup collection, int position, Object view) {
		if (map.containsKey(position)) {
			UrlTouchImageViewForImage v = map.get(position);
			BitmapDrawable drawable = (BitmapDrawable) v.mImageView
					.getDrawable();
			if (drawable != null) {
				Bitmap bmp = drawable.getBitmap();
				if (null != bmp && !bmp.isRecycled()) {
					bmp.recycle();
					bmp = null;
				}
			}
			v.mImageView.setVisibility(View.GONE);
			v.tvProgress.setVisibility(View.VISIBLE);
			map.remove(position);
			v = null;
			collection.removeView((View) view);
			view = null;
		}
	}

	public interface onItemTouchImageViewClickListener {
		public void onClick(UrlTouchImageViewForImage iv);
	}

	public void destory() {
		for (int i : map.keySet()) {
			UrlTouchImageViewForImage v = map.get(i);
			BitmapDrawable drawable = (BitmapDrawable) v.mImageView
					.getDrawable();
			if (drawable != null) {
				Bitmap bmp = drawable.getBitmap();
				if (null != bmp && !bmp.isRecycled()) {
					bmp.recycle();
					bmp = null;

				}
			}
		}
	}
}
